package cn.com.yiqi.wsc.portal.controller.searchAds;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.advertisement.criteria.AdvertisementCriteria;
import cn.com.yiqi.wsc.dao.advertisement.entity.AdvertisementEntity;
import cn.com.yiqi.wsc.enumeration.AdvertisementType;
import cn.com.yiqi.wsc.enumeration.EnableStatus;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.portal.controller.searchAds.ro.AdvertisementRO;
import cn.com.yiqi.wsc.service.advertisement.AdvertisementService;
import cn.com.yiqi.wsc.service.advertisement.PortalAdvertisementService;
import cn.com.yiqi.wsc.service.advertisement.dataobject.QueryAdvertisementDO;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by 苟宇恒 on 2017/2/23.
 * 搜索广告管理
 */
@Controller
@RequestMapping("/searchAds")
public class SearchAdsController extends BaseController {

    @Autowired
    private AdvertisementService advertisementService;
    @Autowired
    private PortalAdvertisementService portalAdvertisementService;
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("searchAds/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Result searchAdList(@RequestBody AdvertisementRO ro) {
        Result result = new Result();
        QueryAdvertisementDO queryAdvertisementDO = new QueryAdvertisementDO();
        if (null != ro.getStatus()) {
            queryAdvertisementDO.setStatus(EnableStatus.getByValue(ro.getStatus()));
        }
        if (null != ro.getType()) {
            queryAdvertisementDO.setType(AdvertisementType.getByValue(ro.getType()));
        }
        PageInfo<AdvertisementEntity> pageInfo = advertisementService.queryList(queryAdvertisementDO);
        result.setStatus(200);
        result.setReturnValue(pageInfo);
        return result;
    }
    @RequestMapping(value = "/adsList", method = RequestMethod.POST)
    @ResponseBody
    public Result AdsList(@RequestBody AdvertisementCriteria advertisement) {
        return portalAdvertisementService.searchAdvertisementList(advertisement);
    }



    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody AdvertisementRO ro) {
        Result result =new Result();
        if(ro.getItemId()==null){
            return   ResultUtil.paramError(result,"必须选择商品");

        }
        if(StringUtils.isEmpty(ro.getPicUrl())){
            return   ResultUtil.paramError(result,"图片地址不能为空");

        }

        AdvertisementCriteria advertisementEntity = new AdvertisementCriteria();
        advertisementEntity.setClickCount(0);
        advertisementEntity.setSoldCount(0);
        advertisementEntity.setEnable(ro.getStatus());
        advertisementEntity.setType(ro.getType());
        advertisementEntity.setPicUrl(ro.getPicUrl());
        advertisementEntity.setItemId(ro.getItemId());
        return advertisementService.insert(advertisementEntity);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody AdvertisementRO ro) {
        AdvertisementEntity advertisementEntity = new AdvertisementEntity();
        advertisementEntity.setClickCount(0);
        advertisementEntity.setSoldCount(0);
        advertisementEntity.setEnable(ro.getStatus());
        advertisementEntity.setType(ro.getType());
        advertisementEntity.setPicUrl(ro.getPicUrl());
        advertisementEntity.setId((long)ro.getId());
        advertisementEntity.setItemId(ro.getItemId());
        return advertisementService.update(advertisementEntity);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody AdvertisementRO ro) {
        return advertisementService.delete(ro.getId());
    }


    @RequestMapping(value = "/upAds", method = RequestMethod.POST)
    @ResponseBody
    public Result upAds(@RequestBody  AdvertisementCriteria advertisement) {
        return portalAdvertisementService.upAdvertisement(advertisement);
    }

}
